﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Linq;
using Moonwish.Silverlight.OsAvatar.Programs;
using Moonwish.Silverlight.OsAvatar.Windows;

namespace Moonwish.Silverlight.OsAvatar.InternalApp
{
    /// <summary>
    /// 类似winodws资源管理器，用于导航层次菜单项，
    /// write by zhlin 2012-02-26.
    /// </summary>
    public partial class Explorer : UserControl
    {
        public Explorer()
        {
            InitializeComponent();

            Loaded += Explorer_Loaded;
        }

        void Explorer_Loaded(object sender, RoutedEventArgs e)
        {
            Loaded -= Explorer_Loaded;

            BindingExplorer();
        }

        /// <summary>
        /// 绑定程序列表。
        /// </summary>
        void BindingExplorer()
        {
            var p = (ProgramWrapper)DataContext;

            if (p != null)
            {
                ListboxExplorer.ItemsSource = p.Children;

                if (p.Children.Count > 0)
                {
                    ListboxExplorer.SelectedIndex = 0;
                }

                var window = (AvatarWindow)Parent;
                window.Caption = ProgramWrapper.GetDesktopPath(p);

                var selected = (ProgramWrapper)ListboxExplorer.SelectedItem;
                btnBack.IsEnabled = p.Parent != null;
                btnForward.IsEnabled = selected != null && selected.Children.Count > 0;
            }
        }

        /// <summary>
        /// 桌面图标按钮事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void BtnDesktopDoubleClick(object sender, EventArgs e)
        {
            var p = (ProgramWrapper)((Button)sender).DataContext;
            if (p.Children.Count > 0)
            {
                DataContext = p;

                BindingExplorer();
            }
            else
            {
                p.Show();
            }
        }

        private void btnForward_Click(object sender, RoutedEventArgs e)
        {
            var p = (ProgramWrapper)ListboxExplorer.SelectedItem;
            if (p != null && p.Children.Count > 0)
            {
                DataContext = p;

                BindingExplorer();
            }
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            var p = ((ProgramWrapper) DataContext).Parent;
            var folderP = p.Children.FirstOrDefault(obj => obj.Children.Count > 0);

            DataContext = p;

            BindingExplorer();

            if (folderP != null)
            {
                ListboxExplorer.SelectedItem = folderP;
            }
        }

        private void ListboxExplorer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selected = (ProgramWrapper)ListboxExplorer.SelectedItem;

            btnForward.IsEnabled = selected != null && selected.Children.Count > 0;
        }
    }
}
